import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import IndexView from "@/views/IndexView.vue";
import HomeView from "@/views/HomeView.vue";
import NewsView from "@/views/NewsView.vue";
import MyClubView from "@/views/MyClubView.vue";
import MessageView from "@/views/MessageView.vue";
import PersonalView from "@/views/PersonalView.vue";
import ActivityDetailView from '@/views/ActivityDetailView.vue'
import AssociationLayout from '@/views/association/AssociationLayout.vue';
import Dashboard from '@/views/association/Dashboard.vue'
import AdminDashBoard from '@/views/admin/AdminDashBoard.vue';
import ClubApproval from '@/views/association/ClubApproval.vue'
import ClubList from '@/views/association/ClubList.vue'
import ActivityApproval from '@/views/association/ActivityApproval.vue'
import ActivityList from '@/views/association/ActivityList.vue'
import SystemNotice from '@/views/association/SystemNotice.vue'
import RegisterView from '@/views/RegisterView.vue'
import UserNoticeView from '@/views/UserNoticeView.vue'
import AdminLayout from '@/views/admin/AdminLayout.vue'
import UserManagement from '@/views/admin/UserManagement.vue'
import PhoneManagement from '@/views/admin/PhoneManagement.vue'
import InviteCodeManagement from '@/views/admin/InviteCodeManagement.vue'
import ClubFinance from '@/views/ClubFinance.vue';
import FinanceManagement from '@/views/association/FinanceManagement.vue';

Vue.use(VueRouter)

// 解决重复导航的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err
    }
  })
}

const routes = [
  {
    path: "/",
    redirect: '/login'
  },
  // 登录
  {
    path: "/login",
    name: "login",
    component: LoginView,
    meta: { requiresAuth: false }
  },
  // 注册
  {
    path: "/register",
    name: "register",
    component: RegisterView
  },
  // 社联
  {
    path: "/association",
    component: AssociationLayout,
    name: 'association',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '控制台概览', roles: [2, 3] }
      },
      {
        path: 'club-approval',
        name: 'ClubApproval',
        component: ClubApproval,
        meta: { title: '社团审核', roles: [2, 3] }
      },
      {
        path: 'club-list',
        name: 'ClubList',
        component: ClubList,
        meta: { title: '社团列表', roles: [2, 3] }
      },
      {
        path: 'activity-approval',
        name: 'ActivityApproval',
        component: ActivityApproval,
        meta: { title: '活动审核', roles: [2, 3] }
      },
      {
        path: 'activity-list',
        name: 'ActivityList',
        component: ActivityList,
        meta: { title: '活动列表', roles: [2, 3] }
      },
      {
        path: 'notice',
        name: 'SystemNotice',
        component: SystemNotice,
        meta: { title: '系统通知', roles: [2, 3] }
      },
      {
        path: 'finance',
        name: 'financeManagement',
        component: FinanceManagement,
      }
    ]
  },
  // 普通用户
  {
    path: '/index',
    name: "index",
    component: IndexView,
    meta: { requiresAuth: true, roles: [1, 2, 3] },
    redirect: '/index/home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: HomeView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'news',
        name: 'news',
        component: NewsView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'myclub/:id',
        name: 'myclub',
        component: MyClubView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'activity/:id',
        name: 'ActivityDetail',
        component: ActivityDetailView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'message',
        name: 'message',
        component: MessageView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'personal',
        name: 'personal',
        component: PersonalView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'notice',
        name: 'notice',
        component: UserNoticeView,
        meta: { roles: [1, 2, 3] }
      },
      {
        path: 'finance/:id',
        name: 'finance',
        component: ClubFinance,
        meta: { roles: [1, 2, 3] }
      }
    ]
  },
  // 超级管理员路由
  {
    path: '/admin',
    component: AdminLayout,
    name: 'admin',
    children: [
      {
        path: 'dashboard',
        name: 'admin-dashboard',
        component: AdminDashBoard,
        meta: { title: '控制台', requireAuth: true, role: 3, roles: [3] }
      },
      {
        path: 'users',
        name: 'admin-users',
        component: UserManagement,
        meta: { title: '用户管理', requireAuth: true, role: 3, roles: [3] }
      },
      {
        path: 'phones',
        name: 'admin-phones',
        component: PhoneManagement,
        meta: { title: '手机号管理', requireAuth: true, role: 3, roles: [3] }
      },
      {
        path: 'invite-codes',
        name: 'admin-invite-codes',
        component: InviteCodeManagement,
        meta: { title: '邀请码管理', requireAuth: true, role: 3, roles: [3] }
      }
    ]
  },

  {
    path: '*',  // 匹配所有未定义的路由
    redirect: '/login'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫 - 简化版本
router.beforeEach((to, from, next) => {
  // 简化条件：如果是登录页或注册页，直接放行
  if (to.name === 'login' || to.name === 'register') {
    next();
    return;
    }

  // 检查是否有token
  const token = localStorage.getItem('token');
  
  if (token) {
    // 已登录，放行
    next();
    } else {
    // 未登录，重定向到登录页
    next({ name: 'login' });
  }
});

export default router
